package com.oa.srms.apps.actions.limit;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.google.gson.Gson;
import com.oa.srms.apps.actions.BasicAction;
import com.oa.srms.apps.business.limit.ILimitInfoService;
import com.oa.srms.apps.business.limit.IPostLimitService;
import com.oa.srms.apps.business.organization.IPostInfoService;
import com.oa.srms.apps.business.organization.ISectionInfoService;
import com.oa.srms.apps.constants.IDEDStates;
import com.oa.srms.apps.entity.limit.LimitInfoValue;
import com.oa.srms.apps.entity.organization.PostInfoValue;
import com.oa.srms.apps.entity.organization.SectionInfoValue;

@SuppressWarnings("serial")
public class PostLimitAction extends BasicAction {
	@Autowired
	private List<LimitInfoValue> allData;
	@Autowired
	private ILimitInfoService limitService;
	@Autowired
	private IPostLimitService postLimitService;
	@Autowired
	private ISectionInfoService sectionService;
	@Autowired
	private IPostInfoService postService;
	@Autowired
	private List<SectionInfoValue> sectionList;
	@Autowired
	private Map<String, Object> map;

	private String str;
	
	private StringBuilder writer = new StringBuilder();
	String liElement = "<input name=\"geo\" id=\"chk_{0}\" value=\"{0}\" type=\"checkbox\" /><label>{1}</label>";
	String liParent = "<input value=\"{0}\"  id=\"chk_{0}\" type=\"checkbox\" class=\"parent\" parent=\"true\" /><label>{1}</label>";

	private Integer postId;
	private Integer sectionId;
	private String limitIds;

	public String execute() throws Exception {
		allData = limitService.queryLimitInfoWithPage(1000000, 1);
		sectionList = sectionService.querySectionInfo(null, IDEDStates.ENABLE);
		setStr(buildTree());
		return SUCCESS;
	}

	public void makeTree(List<LimitInfoValue> childrenList) {
		if (childrenList.size() > 0) {
			writer.append("<ul>");
			for (LimitInfoValue liv : childrenList) {
				writer.append("<li>");
				List<LimitInfoValue> nexts = getChildrenByParent(liv
						.getLimitId());
				boolean hasChildren = nexts.size() > 0;
				if (hasChildren) {
					writer.append("<input value=\""
							+ liv.getLimitId()
							+ "\"  id=\"chk_"
							+ liv.getLimitId()
							+ "\" class=\"parentNode\" parent=\"true\" type=\"checkbox\" /><label>"
							+ liv.getLimitName() + "</label>");
				} else {
					writer.append("<input name=\"geo\" id=\"chk_"
							+ liv.getLimitId() + "\" value=\""
							+ liv.getLimitId()
							+ "\" type=\"checkbox\" /><label>"
							+ liv.getLimitName() + "</label>");
				}
				makeTree(nexts);
				writer.append("</li>");
			}
			writer.append("</ul>");
		}
	}

	public String buildTree() {
		List<LimitInfoValue> list = getChildrenByParent(0);

		writer.append("<div  class=\"treeBody\"><ul id=\"limitInfoTree\" class=\"tree\">");

		for (LimitInfoValue item : list) {
			writer.append("<li>");
			List<LimitInfoValue> children = getChildrenByParent(item
					.getLimitId());
			boolean hasChildren = children.size() > 0;
			if (hasChildren) {
				writer.append("<input value=\""
						+ item.getLimitId()
						+ "\"  id=\"chk_"
						+ item.getLimitId()
						+ "\" class=\"parentNode\" parent=\"true\" type=\"checkbox\" /><label>"
						+ item.getLimitName() + "</label>");
			} else {
				writer.append("<input name=\"geo\" id=\"chk_"
						+ item.getLimitId() + "\" value=\"" + item.getLimitId()
						+ "\" type=\"checkbox\" /><label>"
						+ item.getLimitName() + "</label>");
			}
			makeTree(children);
			writer.append("</li>");
		}
		writer.append("</ul></div>");
		return (writer.toString());
	}

	public List<LimitInfoValue> getChildrenByParent(int parentLimitId) {
		List<LimitInfoValue> result = new ArrayList<LimitInfoValue>();

		for (LimitInfoValue liv : allData) {
			if (liv.getParentId() == parentLimitId)
				result.add(liv);
		}
		return result;
	}

	public void mainActionCalledFromAjax() throws Exception {
		String result = getLeafLimitIdsArrayStringByPostId(postId);
		response.setContentType("text/html; charset=UTF-8");
		response.setHeader("Cache-Control", "no-cache");

		response.getWriter().println(result);
	}

	public String getLeafLimitIdsArrayStringByPostId(int postId)
			throws Exception {
		String limitIdsFromOnePostId = postLimitService.queryByPostId(postId)
				.getLimitIds();

		List<LimitInfoValue> list = limitService
				.queryByLimitIds(limitIdsFromOnePostId);

		StringBuilder result = new StringBuilder("[");
		for (LimitInfoValue limit : list) {
			result.append(limit.getLimitId() + ",");
		}
		int len = result.length();

		return result.substring(0, len - 1) + "]";
	}

	public String addOrModify() throws Exception {
		if (postLimitService.addOrModifyPostLimit(postId, limitIds)) {
			response.getWriter().write("success");
		}
		return null;
	}

	public String queryPostBySection() throws Exception {
		if (sectionId != null) {
			Gson gson = new Gson();
			List<PostInfoValue> postList = postService
					.queryPostInfoBySectionId(sectionId);
			String str = gson.toJson(postList);
			response.setContentType("text/html; charset=UTF-8");
			response.setHeader("Cache-Control", "no-cache");
			response.getWriter().println(str);
		}
		return null;
	}

	public String getStr() {
		return str;
	}

	public void setStr(String str) {
		this.str = str;
	}

	public List<SectionInfoValue> getSectionList() {
		return sectionList;
	}

	public void setSectionList(List<SectionInfoValue> sectionList) {
		this.sectionList = sectionList;
	}

	public Map<String, Object> getMap() {
		return map;
	}

	public void setMap(Map<String, Object> map) {
		this.map = map;
	}
	
	public Integer getPostId() {
		return postId;
	}

	public void setPostId(Integer postId) {
		this.postId = postId;
	}

	public Integer getSectionId() {
		return sectionId;
	}

	public void setSectionId(Integer sectionId) {
		this.sectionId = sectionId;
	}

	public String getLimitIds() {
		return limitIds;
	}

	public void setLimitIds(String limitIds) {
		this.limitIds = limitIds;
	}

}